package com.hky.front_end_monitor.controller;

import com.hky.front_end_monitor.dto.ErrorDTO;
import com.hky.front_end_monitor.dto.ResourceDTO;
import com.hky.front_end_monitor.dto.UserAuDTO;
import com.hky.front_end_monitor.dto.WebVitalDTO;
import com.hky.front_end_monitor.mapper.SysWebVitalMapper;
import com.hky.front_end_monitor.service.impl.SysErrorVitalServiceImpl;
import com.hky.front_end_monitor.service.impl.SysUserServiceImpl;
import com.hky.front_end_monitor.utils.R;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Random;

/**
 * @auther Gpu is all you need
 * @date 2022/8/3
 */
@RestController
@RequestMapping("api/data")
public class DataController {
    @Resource
    SysUserServiceImpl sysUserService;
    @Resource
    SysWebVitalMapper sysWebVitalMapper;
    @Resource
    SysErrorVitalServiceImpl errorVitalService;
    private final Random random = new Random(System.currentTimeMillis());

    @RequestMapping("webVital")
    R getFp(@RequestBody UserAuDTO userAuDTO) {
        if (!sysUserService.isLegitimateUser(userAuDTO)) return R.fail();
        ArrayList<WebVitalDTO> res = (ArrayList<WebVitalDTO>) sysWebVitalMapper.selectWebVitals(userAuDTO.getUserId());
        for (WebVitalDTO re : res) {
            re.setCls(random.nextDouble());
        }
        return R.ok(res);
    }
    @RequestMapping("resource")
    R getResourceInfo(@RequestBody UserAuDTO userAuDTO) {
        if (!sysUserService.isLegitimateUser(userAuDTO)) return R.fail();
        ArrayList<ResourceDTO> res = (ArrayList<ResourceDTO>) sysWebVitalMapper.selectResourceFlows(userAuDTO.getUserId());
        return R.ok(res);
    }
    @RequestMapping("err")
    R getErrInfo(@RequestBody UserAuDTO userAuDTO) {
        if (!sysUserService.isLegitimateUser(userAuDTO)) return R.fail();
        ArrayList<ErrorDTO> res = (ArrayList<ErrorDTO>) errorVitalService.getErrorInfo(userAuDTO.getUserId());
        return R.ok(res);
    }
}
